package cn.yl.order.server;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;

public class GeneraCode {


    @Test
    void testUUid(){
        String uuId = UUID.randomUUID().toString().replace("-", "");
        System.out.println("uuId = " + uuId);
    }

    @Test
    void JSON(){
        ArrayList<String > list = new ArrayList<>();
        list.add("one");
        list.add("two");
        Object json = JSON.toJSON(list);
        String s = json.toString();
        System.out.println("s = " + s);
    }

    @Test
    public void generaCode(){
//        String s = this.getClass().getPackage().getName().replace(".", "/") + "/mapper";
//        System.out.println("s = " + s);
//        String substring = this.getClass().getPackage().getName().split("\\.")[3];
//        System.out.println("substring = " + substring);
        generator();
    }
    void generator(){
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/edu_order?serverTimezone=GMT%2B8", "root", "aptx4869")

                .globalConfig(builder -> {
                    builder.author("yl") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .disableOpenDir()
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir")+"/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(this.getClass().getPackage().getName().substring(0, this.getClass().getPackage().getName().lastIndexOf("."))) // 设置父包名
                            .moduleName(this.getClass().getPackage().getName().split("\\.")[3]) // 设置父包模块名
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"/src/main/java/"+this.getClass().getPackage().getName().replace(".","/")+"/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok()
                            .superClass("cn.yl.api.server.entity.BaseEntity") //设置父模块路径
                            .addTableFills(new Column("createTime", FieldFill.INSERT))
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE));
                    builder.mapperBuilder().enableMapperAnnotation().build();
                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                    builder//
                            .addTablePrefix() // 设置过滤表前缀
                            .addFieldPrefix()
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableChainModel()
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .logicDeleteColumnName("is_del")
                            .logicDeletePropertyName("del")
                            .enableTableFieldAnnotation()
                            .addSuperEntityColumns("id", "create_time","update_time") //需要忽略父模块字段
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseResultMap()  //生成通用的resultMap
                            .superClass(BaseMapper.class)   //设置父类
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
